package com.xxx.service;

import com.xxx.dao.ClaDaoImpl;
import com.xxx.dao.StudentDaoImpl;
import com.xxx.pojo.Cla;
import com.xxx.pojo.Student;
import com.xxx.utils.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/7/29 14:49
 * @Copyright 博为峰
 */
public class ClaStuServiceImpl implements ClaStuService {
    @Override
    public int addClaStu(ArrayList<Cla> cla, ArrayList<Student> stu){
        Connection connection = null;
        try {
            connection = DBUtil.getConnection();
            connection.setAutoCommit(false);//非自动提交
            final ClaDaoImpl claDao = new ClaDaoImpl();
            final ArrayList<Cla> claArrayList = new ArrayList<>();
            claArrayList.add(new Cla("JD119", 100, "2021-07-29 15:24:26"));
            int claId = claDao.transaction(connection, claArrayList);

            final StudentDaoImpl studentDao = new StudentDaoImpl();
            final ArrayList<Student> stuArrayList = new ArrayList<>();
            stuArrayList.add(new Student(100, "岳云鹏3", 36, 170.36, "男", "2021-07-29 15:24:26", claId));
            studentDao.transaction(connection, stu);
            connection.commit();
        } catch (SQLException throwables) {
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        return 0;
    }
}
